<?php
require_once "wp-config.php";
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_select_db(DB_NAME);
mysql_query("SET NAMES utf8"); //connect in decode utf8
//$table_prefix
$bname=gPost('name');
$baddress=gPost('addr');
$bphone=gPost('phone');
$bweb=gPost('web');
$bemail=gPost('email');
$bplus=gPost('plus');
$bcat=gPost('cat');
$a = explode(",", $baddress);
$bcountry = trim($a[2]);
$a1 = explode(" ",trim($a[1]));
$bcity=trim($a1[1]);
$bzip=trim($a1[0]);
//$baddress=trim($a[0]);
$sql="select * from wp_postmeta where meta_key='geo_address' and meta_value='$baddress' limit 1";
$result = mysql_query($sql);
if (!mysql_num_rows($result))
if(strlen($bname)>1){
	$slug=make_post_slug($bname);
	$pid = insert_post($bname,"...",$slug);
	if ($pid>0) {
		if ($bcat!="") {
			add_post_listcat($pid,$bcat);
		}
		post_meta($pid,"geo_address",$baddress);
		post_meta($pid,"geocraft_meta_email",$bemail);
		post_meta($pid,"geocraft_phone",$bphone);
		post_meta($pid,"geocraft_website",$bweb);
		if ($bplus)
			post_meta($pid,"geocraft_googleplus","https://plus.google.com/$bplus/posts");
		post_meta($pid,"geocraft_dummy_content","1");
		post_meta($pid,"geocraft_listing_type","pro");
		//post_meta($pid,"geo_latitude",$baddress);
		//post_meta($pid,"geo_longitude",$baddress);post_meta($pid,"geo_address",$baddress);post_meta($pid,"geo_address",$baddress);
		//post_meta($pid,"geocraft_meta_image1",$baddress);
	}
}
echo "OK";

function gPost($gp)
{
	if (isset($_POST[$gp]))
		return $_POST[$gp];
	else
		return "";
}

function insert_post($title,$content,$slug='') {
	$today = date("Y-m-d H:i:s");
	$sql="INSERT INTO `wp_posts` (`post_author`,`post_date`,`post_date_gmt`,`post_content`,`post_title`,`post_excerpt`,`post_status`,`comment_status`,`ping_status`,`post_password`,`post_name`,`to_ping`,`pinged`,`post_modified`,`post_modified_gmt`,`post_content_filtered`,`post_parent`,`guid`,`menu_order`,`post_type`,`post_mime_type`,`comment_count`) VALUES ('1','$today','$today','".mysql_real_escape_string($content)."','$title','','draft','open','open','','$slug','','','$today','$today','','0','http://www.cfdtradingnews.info/listing/$slug','0','listing','','0');";
	//echo $sql;
	$post_id=0;
	$result = mysql_query($sql);
	/**/
	if ($result) {
		$post_id= mysql_insert_id();
		//$sql = "update wp_posts set guid='http://footygoat.com/blog/?p=.$post_id.' where ID=$post_id;";
		//$result = mysql_query($sql);
	}
	/**/
	return $post_id;
}
function insert_term($term_name,$term_type,$slug='') { //$term_type = category or post_tag
		$sql="select * from `wp_terms` where `name`='$term_name'";
		$result = mysql_query($sql);
		if (mysql_num_rows($result)) {
			//echo $sql;
			$data=mysql_fetch_array($result);
			$cat_id=$data['term_id'];
		} else {
			$sql="INSERT INTO `wp_terms` (`name`,`slug`,`term_group`) VALUES ('$term_name','$slug','0');";
			$result = mysql_query($sql);
			$cat_id= mysql_insert_id();
			//echo $sql;
		}
		$sql="select * from `wp_term_taxonomy` where `term_id`='$cat_id' and `taxonomy`='$term_type'";
		$result = mysql_query($sql);
		if (mysql_num_rows($result)) {
			$data=mysql_fetch_array($result);
			$tt_id=$data['term_taxonomy_id'];
		} else {
			$sql="INSERT INTO `wp_term_taxonomy` (`term_id`,`taxonomy`,`description`,`parent`,`count`) VALUES ('$cat_id','$term_type','','0','1');";
			$result = mysql_query($sql);
			$tt_id= mysql_insert_id();
		}
		//echo "tt$tt_id<br/>";
		return  $tt_id;
}

function add_term_to_post($post_id,$term_name,$term_type) {
	$slug=str_replace(" ","-",$term_name);
	$term_id=insert_term($term_name,$term_type,$slug);
	//echo "tt: $term_id<br/>";
	if ($term_id) {
		$sql="INSERT IGNORE INTO `wp_term_relationships` (`object_id`,`term_taxonomy_id`,`term_order`) VALUES ('$post_id','$term_id','0');";
		$result = mysql_query($sql);
		return mysql_insert_id();
	}
	return 0;
}
function add_post_category($post_id,$cat_name) {
	return add_term_to_post($post_id,$cat_name,'category');
}
function add_post_listcat($post_id,$cat_name) {
	return add_term_to_post($post_id,$cat_name,'listcat');
}
function add_post_tag($post_id,$tag_name) {
	return add_term_to_post($post_id,$tag_name,'post_tag');
}
function post_meta($post_id,$meta, $txt) {
	$sql="INSERT INTO wp_postmeta (post_id,meta_key,meta_value) VALUES('$post_id','$meta','$txt');";
	$result = mysql_query($sql);
}
function slugify0($str) {
	$table = array(
        'Š'=>'S', 'š'=>'s',
		'Đ'=>'Dj', 'đ'=>'dj',
		'Ž'=>'Z', 'ž'=>'z',
		'Č'=>'C', 'č'=>'c',
		'Ć'=>'C', 'ć'=>'c',
        'À'=>'A',
		'Á'=>'A', 'á'=>'a',
		'Â'=>'A', 'â'=>'a',
		'Ã'=>'A', 'ã'=>'a',
		'Ä'=>'A', 'ä'=>'a',
		'Å'=>'A',
		'Æ'=>'A',
		'Ç'=>'C',
		'È'=>'E',
		'É'=>'E',
        'Ê'=>'E',
		'Ë'=>'E',
		'Ì'=>'I',
		'Í'=>'I',
		'Î'=>'I',
		'Ï'=>'I',
		'Ñ'=>'N',
		'Ò'=>'O',
		'Ó'=>'O',
		'Ô'=>'O',
        'Õ'=>'O',
		'Ö'=>'O',
		'Ø'=>'O',
		'Ù'=>'U',
		'Ú'=>'U',
		'Û'=>'U',
		'Ü'=>'U',
		'Ý'=>'Y',
		'Þ'=>'B',
		'ß'=>'Ss',
        'à'=>'a',
		
		'å'=>'a', 
		'æ'=>'a', 
		'ç'=>'c', 
		'è'=>'e', 
		'é'=>'e',
        'ê'=>'e', 
		'ë'=>'e', 
		'ì'=>'i', 
		'í'=>'i', 
		'î'=>'i', 
		'ï'=>'i', 
		'ð'=>'o', 
		'ñ'=>'n', 
		'ò'=>'o', 
		'ó'=>'o',
        'ô'=>'o', 
		'õ'=>'o', 
		'ö'=>'o', 
		'ø'=>'o', 
		'ù'=>'u', 
		'ú'=>'u', 
		'û'=>'u', 
		'ý'=>'y', 
		'ý'=>'y', 
		'þ'=>'b',
        'ÿ'=>'y', 
		'Ŕ'=>'R', 
		'ŕ'=>'r'
    );
	return strtr($str,$table);
}
function slugify($str, $delimiter='-') {
	$clean = slugify0($str);
	if (function_exists('iconv'))
	$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $clean);
	$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
	$clean = strtolower(trim($clean, '-'));
	$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
	return $clean;
}
function make_post_slug($bname) {
	$slug = slugify($bname);
	$noi="-";
	while (true) {
		$sql = "select guid from wp_post where guid='$slug' limit 1";
		$result = mysql_query($sql);
		if (mysql_num_rows($result)) {
			$slug.=$noi.chr(rand(48,57));
			$noi="";
		} else {
			break;
		}
	}
	return $slug;
}
?>